package com.jjsc.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jjsc.client.api.domain.UpdateUserLogin;
import com.jjsc.client.api.domain.UserAddress;
import com.jjsc.client.api.domain.UserInfo;
import com.jjsc.client.mapper.UserAddressMapper;
import com.jjsc.client.service.IUserInfoService;
import com.jjsc.client.mapper.UserInfoMapper;
import com.jjsc.common.core.exception.ServiceException;
import com.jjsc.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 41254
 * @description 针对表【user_info(客户表)】的数据库操作Service实现
 * @createDate 2025-10-16 21:06:47
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {
    @Autowired
    private UserAddressMapper userAddressMapper;

    /**
     * 客户注册
     */
    @Override
    public void register(UserInfo userInfo) {
        long count = baseMapper.selectCount(new
                LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getUsername, userInfo.getUsername())
        );
        if (count > 0) {
            throw new ServiceException("账户已存在");
        }
        baseMapper.insert(userInfo);
    }

    /**
     * 根据用户名查询用户信息
     */
    @Override
    public UserInfo selectUserByUserName(String username) {
        return baseMapper.selectOne(new
                LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getUsername, username));
    }

    @Override
    public Boolean updateUserLogin(UpdateUserLogin updateUserLogin) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(updateUserLogin.getUserId());
        userInfo.setLastLoginIp(updateUserLogin.getLastLoginIp());
        userInfo.setLastLoginTime(updateUserLogin.getLastLoginTime());
        baseMapper.updateById(userInfo);
        return true;
    }

    //==================================================================================================================

    /**
     * 查询客户列表列表
     *
     * @param userInfo 客户列表
     * @return 客户列表
     */
    @Override
    public List<UserInfo> selectUserInfoList(UserInfo userInfo) {
        return baseMapper.selectUserInfoList(userInfo);
    }


    /**
     * 查询客户地址列表
     */
    @Override
    public List<UserAddress> selectUserAddressList(Long userId) {
        return userAddressMapper.selectList(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, userId));
    }
}




